/*
 * 版权所有 © 成都太阳高科技有限责任公司
 * http://www.suncd.com
 */
package com.suncd.demopdf.controller;

import com.alibaba.fastjson.JSON;
import com.suncd.demopdf.Utils.PdfUtils;
import com.suncd.demopdf.vo.OrganizationUserVo;
import com.suncd.demopdf.vo.PlanUserVo;
import com.suncd.demopdf.vo.PreviewPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：pdf预览、下载
 *
 * @author qust
 * @version 1.0 2018/2/23 9:35
 */
@Controller
@RequestMapping(value = "/pdf")
public class PdfController {

    @Autowired
    private FreeMarkerConfigurer configurer;

    /**
     * pdf预览
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @RequestMapping(value = "/preview", method = RequestMethod.GET)
    public void preview(HttpServletRequest request, HttpServletResponse response) {
        // 构造freemarker模板引擎参数,listVars.size()个数对应pdf页数
        List<Map<String, Object>> listVars = new ArrayList<>();
        // Map<String, Object> variables = new HashMap<>();
        // variables.put("title", "测试预览ASGX!");
        // listVars.add(variables);


        PreviewPlanVo previewPlanVo = new PreviewPlanVo();

        previewPlanVo.setSheetTitle("sheetTitle");
        previewPlanVo.setCutoverStartTime("2022-10-14");

        OrganizationUserVo userVo = new OrganizationUserVo();
        userVo.setDepartment("dept");
        userVo.setName("tony");
        userVo.setPost("post");
        userVo.setPhone("183");
        userVo.setPosition("位置");
        previewPlanVo.getOrganizationUserList().add(userVo);

        OrganizationUserVo userVo2 = new OrganizationUserVo();
        userVo2.setDepartment("dept");
        userVo2.setName("tony");
        userVo2.setPost("post");
        userVo2.setPhone("183");
        userVo2.setPosition("位置");
        previewPlanVo.getOrganizationUserList().add(userVo2);

        PlanUserVo planUserVo = new PlanUserVo();
        planUserVo.setOptCooperate("coo");
        planUserVo.setOptUser("tony");
        planUserVo.setOptDesc("desc");
        planUserVo.setOptOrder("order");
        planUserVo.setOptTime("TIME");

        // previewPlanVo.getPlanUserList().add(planUserVo);

        Map<String, Object> dataMap = JSON.parseObject(JSON.toJSONString(previewPlanVo), Map.class);

        listVars.add(dataMap);

        PdfUtils.preview(configurer, "pdfPage.ftl", listVars, response);
    }

    /**
     * pdf下载
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> listVars = new ArrayList<>();
        Map<String, Object> variables = new HashMap<>();
        variables.put("title", "测试下载ASGX!");
        listVars.add(variables);
        PdfUtils.download(configurer, "pdfPage.ftl", listVars, response, "测试中文.pdf");
    }
}
